/**
* Calculator.js  Version 1 - 03/04/2008
* Eduardo Bonfandini eduardo@solis.coop.br
*
* In your page call calcMake('elementId'), to transforma input in a calculator input;
*
* 03/04/2008 - Created.
* 07/11/2009 - Corrected to use in google chrome.
*/

function calcOnKeyDown(e, elementId)
{
    keycode = window.event ? e.keyCode : e.which;

    element = document.getElementById(elementId);

    //alert(keycode);

    if ( element )
    {
        keys = new Array(48,49,50,51,52,53,54,55,56,57, 96,97,98,99,100,101,102,103,104,105, 106,107,108,109,110,111, 8,46, 37,38,39,40, 188,190);

        ok   = false;

        for ( i = 0 ; i < keys.length ; i ++)
        {
            key = keys[i];

            if (key == keycode)
            {
                ok = true;
            }
        }

        if ( keycode == 27 )
        {
            //alert('esc');
            element.value = '';
            //calcClear(elementId);
            return false;
        }

        if ( keycode == 13 || keycode == 9)
        {
            calculate(elementId);

            if ( keycode == 13 )
            {
                return false;
            }
            else
            {
                return true; //tab return true
            }
        }

        if (ok)
        {
            calcShow(elementId);
            return true;
        }
        else
        {
            return false;
        }

    }
    else
    {
        alert('Element \''+elementId +'\' does not exists.');
        return false;
    }
}

function calculate(elementId)
{
    //TODO make better error handler
    element = document.getElementById(elementId);
    value   = element.value;
    value   = value.replace(',','.'); //changes ',' by '.'
    if (value == '')
    {
        value = 0;
    }
    eval('element.value='+value+';');
    return false;
}

function calcShow(elementId)
{
    calcId  = elementId + 'calc';
    element = document.getElementById(elementId);
    calc    = document.getElementById(calcId);

    calc.style.width    = element.clientWidth;
    calc.style.display  = 'block';
    calc.style.left     = element.offsetLeft;
    calc.style.top      = element.offsetTop + element.clientHeight;

    return false;
}

function calcHide(elementId)
{
    calcId              = elementId + 'calc';
    calc                = document.getElementById(calcId);
    calc.style.display  = 'none';

    return false;
}

function calcAdd(elementId, value)
{
    element = document.getElementById(elementId);

    element.value += value;

    return false;
}

function calcClear(elementId)
{
    element = document.getElementById(elementId);

    element.value = '';

    return false;
}

function calcMake(elementId)
{
    element = document.getElementById(elementId);

    if (element)
    {
        element.setAttribute('onkeydown', "return calcOnKeyDown(event, '"+elementId+"');");
        element.setAttribute('onclick', "calcShow('"+elementId+"');");
        element.setAttribute('onblur', "calculate('"+elementId+"');");

        body = document.getElementsByTagName('body')[0];

        body.innerHTML = body.innerHTML +
        "<div id='"+elementId+"calc' style='background-color:lightGrey; position: absolute; display: none; border: solid 1px gray;' >"+
        "<button onclick=\"return calcAdd('"+elementId+"', '1');\">1</button>"+
        "<button onclick=\"return calcAdd('"+elementId+"', '2');\">2</button>"+
        "<button onclick=\"return calcAdd('"+elementId+"', '3');\">3</button>"+
        "<button onclick=\"return calcAdd('"+elementId+"', '4');\">4</button>"+
        "<button onclick=\"return calcAdd('"+elementId+"', '5');\">5</button>"+
        "<button onclick=\"return calcAdd('"+elementId+"', '6');\">6</button>"+
        "<button onclick=\"return calcAdd('"+elementId+"', '7');\">7</button>"+
        "<button onclick=\"return calcAdd('"+elementId+"', '8');\">8</button>"+
        "<button onclick=\"return calcAdd('"+elementId+"', '9');\">9</button>"+
        "<button onclick=\"return calcAdd('"+elementId+"', '0');\">0</button>"+
        "<button onclick=\"return calcAdd('"+elementId+"', '+');\">+</button>"+
        "<button onclick=\"return calcAdd('"+elementId+"', '-');\">-</button>"+
        "<button onclick=\"return calcAdd('"+elementId+"', '*');\">*</button>"+
        "<button onclick=\"return calcAdd('"+elementId+"', '/');\">/</button>"+
        "<button onclick=\"return calcAdd('"+elementId+"', '.');\">.</button>"+
        "<button onclick=\"return calculate('"+elementId+"');\">=</button>"+
        "<button onclick=\"return calcClear('"+elementId+"');\">C</button>"+
        "<button onclick=\"return calcHide('"+elementId+"');\">X</button>"+
        "</div>";

    }
    else
    {
        alert("Element "  + elementId +" does not exists.");
    }
}
